﻿#include "widget.h"
#include "ui_widget.h"



Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    socket = new QTcpSocket;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_connectPushButton_clicked()
{
    QString ip = ui->ipLineEdit->text();
    QString port = ui->portLineEdit->text();

    //连接服务器
    socket->connectToHost(QHostAddress(ip),port.toShort());
    //如果连接成功,socket对象发出信号
    connect(socket,&QTcpSocket::connected,[this](){
        QMessageBox::information(this,"提示","服务器连接成功!");

        this->hide();
        //ChatForm chatForm;//局部变量在栈控件创建,函数运行完毕就释放掉了.
        ChatForm *c = new ChatForm(socket);//在堆控件创建对象
        c->show();

    });


    //如果连接断开,
    connect(socket,&QTcpSocket::disconnected,[this](){
        QMessageBox::information(this,"提示","服务器连接异常,网络断开!");
    });

}



void Widget::on_cacelPushButton_clicked()
{
    socket->disconnected();
}

